package com.wisedu.w5autocode.util;

import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;

import com.wisedu.w5autocode.wizards.ColumnLayout;

public class SwtUtils {

	public static Label createLabel(Composite parent, String name) {
		Label label = new Label(parent, SWT.NULL);
		label.setText(name);
		label.setLayoutData(new GridData());
		return label;
	}

	public static Text createText(Composite parent) {
		Text text = new Text(parent, SWT.BORDER | SWT.SINGLE);
		text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		return text;
	}

	public static Button createCheckbox(Composite parent, String name) {
		Button button = new Button(parent, SWT.CHECK);
		button.setText(name);
		button.setLayoutData(new GridData());
		return button;
	}

	public static int computeMinimumColumnWidth(GC gc, String string) {
		return gc.stringExtent(string).x + 10;
	}

	public static TableColumn createTableColumn(Table table, String label, GC gc, ColumnLayout columnLayout) {
		TableColumn column = new TableColumn(table, SWT.NONE);
		column.setText(label);
		int minWidth = SwtUtils.computeMinimumColumnWidth(gc, label);
		columnLayout.addColumnData(new ColumnWeightData(1, minWidth, true));
		return column;
	}
}
